package com.njust.controller;

import com.njust.common.Const;
import com.njust.common.TokenCache;
import com.njust.dataobject.User;

import com.njust.service.UserService;
import com.njust.vo.PasswordVO;
import com.njust.vo.ResultVO;
import com.njust.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 用户管理模块
 * @author zjwpro@foxmail.com
 * @date 2018/1/9 下午2:25
 */
@RestController
@RequestMapping("/user")
@Api("用户模块")
@Slf4j
public class UserContronller{

    @Autowired
    private UserService userService;


    @GetMapping("/hello")
    public ResultVO hello(){
        return ResultVO.createByErrorMessage("请先登入！");
    }

    /**
     * 登入
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value="/login", notes="登入")
    public ResultVO login(@RequestBody User user, HttpSession session, HttpServletResponse response){
        UserVO userVO=new UserVO();
        User u = userService.login(user);
        if(u==null){
            return ResultVO.createByErrorMessage("用户名或者密码错误");
        }
        BeanUtils.copyProperties(u,userVO);
        Map<String,Object> map=new HashMap<>();
        //生成token
        String Token= UUID.randomUUID().toString();
        //存放到缓存中
        TokenCache.setKey(TokenCache.TOKEN_PREFIX+u.getUsername(),Token);
        log.info("token_key={}",TokenCache.TOKEN_PREFIX+u.getUsername());
        log.info("token_value={}",TokenCache.getKey(TokenCache.TOKEN_PREFIX+u.getUsername()));
        map.put("user",userVO);
        map.put("token",Token);
        //存放登入信息到session中
        session.setAttribute(Const.CURRENT_USER,u.getUsername());
        response.addHeader("token",Token);
        return ResultVO.createBySuccess("登入成功",map);
    }
    /**
     * 登出用户
     * @param session
     */
    @GetMapping("/logout")
    @ApiOperation(value="/logout", notes="登出")
    public ResultVO logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ResultVO.createBySuccessMessage("登出成功");
    }

    /**
     * 注册或添加
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value="/register", notes="注册")
    public ResultVO register(@RequestBody User user){
        return userService.save(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value="/update", notes="更新用户")
    public ResultVO update(@RequestBody User user){
        ResultVO resultVO = userService.update(user);
        return resultVO;
    }

    /**
     * 修改密码
     * @param passwordVO
     * @return
     */
    @PostMapping("/password")
    @ApiOperation(value="/password", notes="修改密码")
    public ResultVO modify(@RequestBody PasswordVO passwordVO){
        ResultVO resultVO = userService.modifyPassword(passwordVO);
        return resultVO;
    }

    @GetMapping("/allusers")
    @ApiOperation(value="/allusers", notes="管理员查询所有用户")
    public ResultVO findAllUser(){
        ResultVO resultVO = userService.findAllUsers();
        return resultVO;
    }
}
